package com.sls.api.util;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

/**
 * 数据处理
 * 
 * @author cx
 * 
 */
public class DataDeal {

	private static Logger log = Logger.getLogger(DataDeal.class);

	/**
	 * 数据转换（元到厘）
	 * 
	 * @param str
	 * @return
	 */
	public static int converStringToInteger(String str)
			throws NumberFormatException {
		int value = 0;
		if (StringUtils.isNotEmpty(str)) {
			double doubleValue = Double.parseDouble(str);
			doubleValue = doubleValue * 1000;
			long l = Math.round(doubleValue);
			Long lo = new Long(l);
			value = lo.intValue();
		}

		return value;
	}

	/**
	 * 数据转换（厘到元）
	 * 
	 * @return
	 */
	public static String converIntegerToString(String str) {
		String value = null;
		if (str != null && !("".equals(str))) {
			double douValue = Double.parseDouble(str);
			double douValue1 = douValue / 1000;
			value = douValue1 + "";
		}

		return value;
	}

	/**
	 * 校验总价是否相同
	 * 
	 * @param buyNum
	 * @param unitPrice
	 * @param totalPrice
	 * @return
	 * @throws IOException
	 */
	public static boolean checkTotalPrice(String buyNum, String unitPrice,
			String totalPrice) {
		boolean result = false;

		try {
			int buy = Integer.parseInt(buyNum);
			int unitP = DataDeal.converStringToInteger(unitPrice);
			int total = DataDeal.converStringToInteger(totalPrice); //订单金额
			int temp = buy * unitP;
			if (temp <= total) {
				result = true;
			} else {
				result = false;
			}

		} catch (NumberFormatException e) {
			log.info("交易时,格式转换失败!!!");
			return false;
		}

		return result;

	}
	
}
